# Author:Li
import redis
from django import forms
from captcha.fields import CaptchaField

from online_class.settings import REDIS_HOST, REDIS_PORT

from apps.users.models import UserProfile


class UpdateMobileForm(forms.Form):
    """登录"""
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    mobile_code = forms.CharField(required=True, min_length=4, max_length=4)

    def clean_code(self):
        """验证"""
        mobile = self.data.get("mobile")
        code = self.data.get("mobile_code")
        r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password="123456", db=0, charset="utf8",
                        decode_responses=True)
        redis_code = r.get(str(mobile))
        if code != redis_code:
            raise forms.ValidationError("验证码不正确")

        return self.cleaned_data

    # def clean(self):
    #     mobile = self.cleaned_data["mobile"]
    #     code = self.cleaned_data["code"]
    #     r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password="123456", db=0, charset="utf8",
    #                     decode_responses=True)
    #     redis_code = r.get(str(mobile))
    #     if code != redis_code:
    #         raise forms.ValidationError("验证码不正确")
    #
    #     return self.cleaned_data


class ChangePwdForm(forms.Form):
    """修改密码的表单验证"""
    password1 = forms.CharField(required=True, min_length=5)
    password2 = forms.CharField(required=True, min_length=5)

    def clean(self):
        pwd1 = self.cleaned_data["password1"]
        pwd2 = self.cleaned_data["password2"]

        if pwd1 != pwd2:
            raise forms.ValidationError("密码不一致")
        return self.cleaned_data


class UserInfoForm(forms.ModelForm):
    """修改用户个人信息"""
    class Meta:
        model = UserProfile
        fields = ["nick_name", "gender", "birthday", "address"]


class UploadImageForm(forms.ModelForm):
    """头像上传表单验证"""
    class Meta:
        model = UserProfile
        fields = ["image"]


class RegisterGetForm(forms.Form):
    """动态验证码"""
    captcha = CaptchaField()


class RegisterPostForm(forms.Form):
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    code = forms.CharField(required=True, min_length=4, max_length=4)
    password = forms.CharField(required=True, min_length=6, max_length=20)

    def clean_mobile(self):
        mobile = self.data.get("mobile")
        # 验证手机号码是否已经注册
        users = UserProfile.objects.filter(mobile=mobile)
        if users:
            raise forms.ValidationError("手机号码已注册")
        return mobile

    def clean_code(self):
        mobile = self.data.get("mobile")
        code = self.data.get("code")
        r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password="123456", db=0, charset="utf8",
                        decode_responses=True)
        redis_code = r.get(str(mobile))
        if code != redis_code:
            raise forms.ValidationError("验证码不正确")

        return code


class LoginForm(forms.Form):
    username = forms.CharField(required=True, min_length=2)
    password = forms.CharField(required=True, min_length=6)
    # required=True 是否必填；username等变量必须要和前端input的name值一致。


class DynamicLoginForm(forms.Form):
    """发送手机验证码"""
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    captcha = CaptchaField()


class DynamicLoginPostForm(forms.Form):
    """登录"""
    mobile = forms.CharField(required=True, min_length=11, max_length=11)
    code = forms.CharField(required=True, min_length=4, max_length=4)

    def clean_code(self):
        """验证"""
        mobile = self.data.get("mobile")
        code = self.data.get("code")
        r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password="123456", db=0, charset="utf8",
                        decode_responses=True)
        redis_code = r.get(str(mobile))
        if code != redis_code:
            raise forms.ValidationError("验证码不正确")

        return self.cleaned_data

    # def clean(self):
    #     mobile = self.cleaned_data["mobile"]
    #     code = self.cleaned_data["code"]
    #     r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password="123456", db=0, charset="utf8",
    #                     decode_responses=True)
    #     redis_code = r.get(str(mobile))
    #     if code != redis_code:
    #         raise forms.ValidationError("验证码不正确")
    #
    #     return self.cleaned_data


